<?php
/**
 * Created by PhpStorm.
 * User: fuguang
 * Date: 2017/6/2
 * Time: 上午9:28
 */

namespace common\controllers;


use yii\web\BadRequestHttpException;
use yii\web\Controller;
use Yii;
use yii\web\HttpException;

class BaseController extends Controller
{

    /**
     * @inheritdoc
     */
    public function beforeAction($action)
    {
        $module = Yii::$app->controller->module->id;
        $ctl = Yii::$app->controller->id;
        $actionname = Yii::$app->controller->action->id;

        $actionPath =  strtolower("/{$ctl}/{$actionname}");//具体action
        $controllerPath = strtolower("／{$ctl}/*");//controller
        $modulePath = strtolower("/{$module}/*");
        $sitePath = "／*";//整站
        if(Yii::$app->user->can($sitePath) || Yii::$app->user->can($modulePath) ||
            Yii::$app->user->can($controllerPath) || Yii::$app->user->can($actionPath)){
            if (parent::beforeAction($action)) {
                return true;
            }
        }else{
            //throw new BadRequestHttpException('对不起，您现在还没获此操作的权限',403);
            throw new HttpException(403,Yii::t('yii', 'You are not allowed to perform this action.'));
        }

        return false;
    }
}